/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.defaults;

import java.util.logging.Level;
import org.balisunrise.common.Logger;

/**
 *
 * @author glauco
 */
public class DefaultLogger implements Logger<Exception>{

    private static final String NEW_LINE = "\n";

    @Override
    public void log(String text) {
        log(null,text);
    }

    @Override
    public void log(Level level, String text) {
        log(level,text,null);
    }

    @Override
    public void log(Level level, String text, Throwable ex) {
        log(null,level,text,ex);
    }

    @Override
    public void log(Class cls, Level level, String text, Throwable ex) {

        StringBuilder builder = new StringBuilder();

        if (cls != null)
            builder.append("Logger for class: ")
                    .append(cls.getName())
                    .append(NEW_LINE);

        if(level != null)
            builder.append(level.toString())
                    .append(": ");
        else
            builder.append("LOG: ");

        if(text != null)
            builder.append(text);
        else
            builder.append("NO MESSAGE");

        if(ex != null){
            builder.append(NEW_LINE)
                    .append(ex.getMessage());
            for (StackTraceElement ste : ex.getStackTrace()) {
                builder.append(NEW_LINE)
                        .append(ste.getClassName())
                        .append("#")
                        .append(ste.getMethodName())
                        .append(" line: ")
                        .append(ste.getLineNumber());
            }
        }

        System.out.println(builder.toString());
    }

    @Override
    public void log(Exception log) {
        log(null,null,null,log);
    }

}
